package com.zxl.yjb.activity.home;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zxl.yjb.R;
import com.zxl.yjb.adapter.MyFragmentPagerAdapter;
import com.zxl.yjb.adapter.SkillSubmitAdapter;
import com.zxl.yjb.adapter.SkillTabAdapter;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.bean.MessageEvent;
import com.zxl.yjb.bean.SkillClassifyBean;
import com.zxl.yjb.fragment.home.SkillFragment;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.SaveBeanUtil;
import com.zxl.yjb.utils.ToastUtils;
import com.zxl.yjb.widget.NoScrollViewPager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.widget.TabView;

public class SkillRegisterActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_page_title)
    TextView tv_page_title;
    @BindView(R.id.edtTxt_amcham_name)
    TextView edtTxt_amcham_name;
    @BindView(R.id.iv_delete)
    ImageView iv_delete;
    @BindView(R.id.ll_search)
    LinearLayout ll_search;
    @BindView(R.id.tv_skill_classify)
    VerticalTabLayout tv_skill_classify;
    @BindView(R.id.vp_skill)
    NoScrollViewPager vp_skill;
    @BindView(R.id.rv_skill_select)
    RecyclerView rv_skill_select;
    @BindView(R.id.tv_skill_submit)
    TextView tv_skill_submit;


    private ArrayList<Fragment> fragmentList = new ArrayList<>();
    private MyFragmentPagerAdapter skillPagerAdapter;

    private ArrayList<SkillClassifyBean> skillClassifyBeans = new ArrayList<>();
    private SkillTabAdapter skillTabAdapter;

    public ArrayList<SkillClassifyBean.SkillBean> submitSkillList = new ArrayList<>();
    public SkillSubmitAdapter submitSkillAdapter;
    private LinearLayoutManager linearLayoutManager;

    private LoginSucessBean loginSucessBean;

    private DataManager dataManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_skill_register);
        ButterKnife.bind(this);
        loginSucessBean = (LoginSucessBean) SaveBeanUtil.getBean(SkillRegisterActivity.this, Const.VIPINFO);
        initSkill();
        initView();
    }

    @Override
    protected void initView() {
        tv_page_title.setText("请选择您的技能");
        edtTxt_amcham_name.setHint("搜索技能");
        skillPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        vp_skill.setOffscreenPageLimit(4);
        vp_skill.setAdapter(skillPagerAdapter);
        tv_skill_classify.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                vp_skill.setCurrentItem(position, false);
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });

        linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
        rv_skill_select.setLayoutManager(linearLayoutManager);
        submitSkillAdapter = new SkillSubmitAdapter(R.layout.item_submit_skill,submitSkillList);
        rv_skill_select.setAdapter(submitSkillAdapter);

        getSkill();

    }


    private void initSkill()
    {
        if (loginSucessBean!=null&&loginSucessBean.getSkill_name_arr()!=null&&loginSucessBean.getSkill_name_arr().size()>0)
        {
            submitSkillList.addAll(loginSucessBean.getSkill_name_arr());
        }

    }


    private void getSkill() {
        dataManager = new DataManager();
        ObserverOnNextListener<List<SkillClassifyBean>> listener = new ObserverOnNextListener<List<SkillClassifyBean>>() {
            @Override
            public void onNext(List<SkillClassifyBean> skillClassifyBeanList) {
                skillClassifyBeans.clear();
                skillClassifyBeans.addAll(skillClassifyBeanList);
                fragmentList.clear();
                for (int i = 0; i < skillClassifyBeanList.size(); i++) {
                    SkillFragment skillFragment = new SkillFragment();
                    skillFragment.setData(skillClassifyBeans.get(i).getSub());
                    fragmentList.add(skillFragment);
                }
                skillTabAdapter = new SkillTabAdapter(skillClassifyBeans);
                tv_skill_classify.setTabAdapter(skillTabAdapter);
                skillPagerAdapter.notifyDataSetChanged();
            }
        };

        dataManager.getclassify(new ProgressObserver<ArrayList<SkillClassifyBean>>(listener, SkillRegisterActivity.this), new HashMap<String, String>());
    }

    @OnClick({R.id.tv_skill_submit,R.id.iv_back})
    void onClick(View view)
    {
        switch (view.getId()) {
        	case R.id.tv_skill_submit:
                submitSkill();
        		break;

            case  R.id.iv_back:
                finish();
                break;

        	default:
        		break;
        }
    }

    private void submitSkill()
    {
        if (submitSkillList.size() == 0)
        {
            ToastUtils.show(SkillRegisterActivity.this,"请选择要注册的技能");
            return;
        }

        String skillIds = "";
        for (int i = 0; i < submitSkillList.size(); i++) {
            skillIds += submitSkillList.get(i).getId()+",";
        }

        skillIds = skillIds.substring(0,skillIds.length()-1);

        dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("skill",skillIds);
        ObserverOnNextListener<String> listener = new ObserverOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                EventBus.getDefault().post(new MessageEvent("refreshUserInfo"));
                ToastUtils.show(SkillRegisterActivity.this,"技能注册成功");
                finish();
            }
        };
        dataManager.regSkill(new ProgressObserver<String>(listener, SkillRegisterActivity.this), map);
    }
}
